<?php

namespace backend\controllers;

use Yii;
use app\models\Engine;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * MdspartsController implements the CRUD actions for Mdsparts model.
 */
class EngineController extends Controller
{
    public $layout = "content_parts";

    public function actionList(){


        $searchModel = new Engine();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('list', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    public function actionAjax_search(){

        $res = [
            'status'=>0
        ];

        $get = Yii::$app->request->get();
        unset($get['r']);
        $res['status'] = 1;
        $find = Engine::find();
        $where = [];
        foreach( $get as $key => $value ){
//            $where[] = ['like', $key, '%' . $value . '%'];
            $where = ['like', $key,  $value ];
            $find->andOnCondition( $where );
        }

        $res['engines'] = $find->limit(20)->asArray()->all();
        header("Content-type:application/json;charset=utf-8");
        echo json_encode( $res );
    }

    public function actionView($id){

        try{

            $model = Engine::findModel($id);

        }catch( Exception $e ){
            echo $e->getMessage();
            die();
        }

        return $this->render('view', [
            'model' => $model,
        ]);

    }

    public function actionCreate(){

        $model = new Engine();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }

    }

    public function actionUpdate( $id ){
        $model = Engine::findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    public function actionDelete( $id ){
        Engine::findModel($id)->delete();
        return $this->redirect(['list']);
    }

}
